package com.gd.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gd.common.entities.S_ResBottle;
import com.gd.common.entities.S_User;
import com.gd.common.exceptions.BusinessException;
import com.gd.common.util.ConnectionUtil;
import com.gd.common.util.ErrorMessageUtil;
import com.gd.server.dao.ResBottleDao;

public class ResBottleDaoImpl implements ResBottleDao {

	@Override
	public void insertResBottle(int bottleId, int fromUserId, int toUserId,
			String message, int type) throws Exception {

		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_insert = "INSERT INTO gds_res_bottle(bottle_id ,from_user_id , to_user_id, message,time,type) "
				+ "values(?,?,?,?,?,?)";
		pstm = conn.prepareStatement(sql_insert);
		pstm.setInt(1, bottleId);
		pstm.setInt(2, fromUserId);
		pstm.setInt(3, toUserId);
		pstm.setString(4, message);
		pstm.setDouble(5, new Date().getTime());
		pstm.setInt(6, type);
		pstm.execute();
	}


	@Override
	public List<S_ResBottle> getOflineBottle(int userId) throws Exception {
		
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_select = "SELECT * FROM `gds_res_bottle` WHERE `to_user_id`=?";
		pstm = conn.prepareStatement(sql_select);
		pstm.setInt(1, userId);
		rs = pstm.executeQuery();
		
		List<S_ResBottle> resBottle = new ArrayList<S_ResBottle>();
		while(rs.next()){
			S_ResBottle bottle = new S_ResBottle();
			bottle.setId(rs.getInt(1));
			bottle.setBottle_id(rs.getInt(2));
			bottle.setFrom_user_id(rs.getInt(3));
			bottle.setTo_user_id(rs.getInt(4));
			bottle.setMessage(rs.getString(5));
			bottle.setTime((long)rs.getDouble(6));
			bottle.setType(rs.getInt(7));
			resBottle.add(bottle);
		}
		return resBottle;
	}

	
	
}
